package com.hwua.view;

import com.hwua.entity.Teacher;
import com.hwua.service.TeacherService;
import com.hwua.service.impl.TeacherServiceImpl;
import com.hwua.tqm.FatherTable;

import java.util.List;

/**
 * @author CQY
 */
public class TeacherView extends FatherTable {

    /**
     * 设置表格的列名
     * @return
     */
    @Override
    public String[] columnNamesInit() {
        String[] s={"教师编号","教师姓名"};
        return s;
    }

    /**
     * 数据初始化
     * @return
     */
    @Override
    public Object[][] tableDataInit() {
        TeacherService service=new TeacherServiceImpl();
        List<Teacher> list=service.findAll();
        int row=list.size();
        Object[][] date=new Object[row][2];
        for (int i = 0; i <date.length ; i++) {
            Teacher teacher=list.get(i);
            date[i][0]=teacher.getTeaId();
            date[i][1]=teacher.getTeaName();
        }
        return date;
    }

    /**
     * 单选的删除
     * @param ids
     */
    @Override
    public void deleteMoreAction(Object[] ids) {
        TeacherService service=new TeacherServiceImpl();
        for (int i = 0; i <ids.length ; i++) {
            Teacher teacher=new Teacher();
            service.delete((String)ids[i]);
        }


    }

    @Override
    public void addSaveAction(Object[] obj) {
        TeacherService service=new TeacherServiceImpl();
        String id=(String) obj[0];
        String name=(String)obj[1];
        Teacher teacher=new Teacher(id,name);
        int row=service.save(teacher);
    }

    /**
     * 修改信息
     * @param obj
     */
    @Override
    public void updateSaveAction(Object[] obj) {

        TeacherService service=new TeacherServiceImpl();
        String id=(String) obj[0];
        String name=(String)obj[1];
        Teacher teacher=new Teacher(id,name);
        int row=service.update(teacher);

    }

    @Override
    public Object[][] searchAction(String keyword) {
        TeacherService service=new TeacherServiceImpl();
        List<Teacher> list=service.findSearch(keyword);
        int row=list.size();
        Object[][] date=new Object[row][2];
        for (int i = 0; i <date.length ; i++) {
            Teacher teacher=list.get(i);
            date[i][0]=teacher.getTeaId();
            date[i][1]=teacher.getTeaName();
        }
        return date;
    }

    @Override
    public void deleteAction(Object id) {

        TeacherService service=new TeacherServiceImpl();
        service.delete((String)id);
    }
}
